package com.mdxl.layer_cj.test;

import org.junit.Test;

import javax.xml.bind.SchemaOutputResolver;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Lambda {

    //针对第一种方法
    @Test
    public void test1(){
        //首先创建一个
        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,24,"水瓶座")
        );

        List<Student> result = new ArrayList<>();
        for (Student student:list){
            if ("天秤座".equals(student.getStar())){
                result.add(student);
            }
        }
        System.out.println(result);
    }
    public List<Student> filterStudent(List<Student> students, FilterProcess<Student> mp){
        List<Student> list = new ArrayList<>();

        for (Student student : students) {
            if(mp.process(student)){
                list.add(student);
            }
        }
        return list;
    }

    @Test
    public void test2(){
        List<Student> students = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,24,"水瓶座")
        );

        List<Student> list = filterStudent(students, new FilterProcess<Student>() {
            @Override
            public boolean process(Student student) {
                return student.getStar().equals("天秤座");
            }
        });
        for (Student student : list) {
            System.out.println(student);
        }
    }

    @Test
    public void test3(){
        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,24,"水瓶座")
        );
        List<Student> result = filterStudent(list,(e)->e.getStar().equals("天秤座"));
        System.out.println(result);
    }

    @Test
    public void test5(){
        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,24,"水瓶座")
        );

        list.stream()
            .filter((e)->e.getStar().equals("天秤座"))
            .forEach(System.out::println);
    }


    @Test
    public void test4(){


        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,24,"水瓶座")
        );

        list.stream().forEach(student -> {
            if (student.getAge()>=18){
                student.setSalary(10000);
                System.out.println(student);
            }
            else
                System.out.println("小孩");
        });
        System.out.println("=================");
        list.stream()
            .filter((e) -> e.getAge() >17)
            .filter((e) -> e.getName().startsWith("十"))
            .forEach(System.out::println);
        System.out.println("=================");
        list.stream()
            .map(Student::getName)
            .forEach(System.out::println);
        System.out.println("=================");

        Map<String,Map<String,List<Student>>> map =list.stream()
            .collect(Collectors.groupingBy((student)->{
            if(student.getAge()>24)
                return "首先";
            else if(student.getAge()>17)
                return "然后";
            else
                return "最后";
        }, Collectors.groupingBy(Student::getSex)
        ));
        System.out.println(map);
    }

    @Test
    public void test6(){
        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,34,"水瓶座"),
                new Student("十二种","男",3000,44,"水瓶座"),
                new Student("十三娘","男",3000,4,"水瓶座")
        );
        Collections.sort(list,(e1,e2) -> {
            if (e1.getAge() == e2.getAge()){
                return e1.getName().compareTo(e2.getName());
            }else{
                return Integer.compare(e1.getAge(),e2.getAge());
            }
        });
        list.forEach(System.out::println);
    }

    //产生10个随机整数
    @Test
    public void test7(){
        List<Integer> result =randMath(10,()->(int)(Math.random()*100));
        result.forEach(System.out::println);
    }
    public List<Integer> randMath(int a, Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<a;i++){
            Integer m =supplier.get();
            list.add(m);
        }
        return list;
    }
    @Test
    public void test8(){
        String str = handle("hello",(e)->e.replace("h","o"));
        System.out.println(str);
    }
    public String handle(String str, Function<String,String> function){
        return function.apply(str);
    }

    @Test
    public void test9(){
        List<Student> list = Arrays.asList(
                new Student("九天","男",5000,18,"天秤座"),
                new Student("十夜","男",4000,16,"双鱼座"),
                new Student("十一郎","男",3000,34,"水瓶座"),
                new Student("十二种","男",3000,44,"水瓶座"),
                new Student("十三娘","男",3000,4,"水瓶座")
        );
        /*Optional<Integer> optional = list.stream().map(Student::getSalary).min(Double::compare);
        Optional<Integer> sum = list.stream().map(Student::getSalary).reduce(Integer::sum);
        List<String> listName = list.stream().map(Student::getName).collect(Collectors.toList());
        list.stream().map(Student::getName).collect(Collectors.toCollection(HashSet::new));
        list.stream().filter(e->e.getAge()>18).forEach(System.out::println);
        list.stream().collect(Collectors.maxBy((e1,e2)-> Integer.compare(e1.getSalary(),e2.getSalary())));*/
        list.stream().sorted((e1,e2)->{
            if (e1.getAge()==e2.getAge()){
                return e1.getName().compareTo(e2.getName());
            }else{
                return Integer.compare(e2.getAge(),e1.getAge());
            }
        }).forEach(System.out::println);
    }

    @Test
    public void test10(){
        List<Integer> list = Arrays.asList(12,21,34,21,21,5769,32,45,45,87,352,98);
        list.stream()
            .sorted(Comparator.reverseOrder())
            .distinct()
            .forEach(System.out::println);
    }

    @Test
    public void test11(){
        String str    = "BEA";
        String result = str.replace("A","E");
        System.out.println(str.toLowerCase());
        System.out.println(result.toLowerCase());
    }

    @Test
    public void test12(){
        String s1  = "abaddff";
        String s2  = "baaffdd";
        String str1 = sort(s1);
        String str2 = sort(s2);
        Boolean bo  = isEquals(str1,str2);
       if (bo){
           System.out.println("相等");
       }else {
           System.out.println("不相等");
       }

    }
    public static String sort(String ss){
        char[] chars = ss.toCharArray();
        List<String> list = new ArrayList<>();
        for (int i = 0; i<chars.length;i++){
            list.add(String.valueOf(chars[i]));
        }
        List<String> result = list.stream().sorted().collect(Collectors.toList());
        String results ="";
        for (String str : result){
            results+=str;
        }
        return results;
    }
    public static Boolean isEquals(String s1,String s2){
        if (s1.equals(s2)){
            return true;
        }
        return false;
    }
    @Test
    public void test14(){
        String s1 = new String();
        String s2 = "";
        String s3 = new String("first");
        String s4 = "A".intern();
    }

    @Test
    public void test15(){
        String s = new String("add");
        String result = appensStr(s);
        System.out.println(result);
    }
    public static String appensStr(String s){
        s+="abb";
        return s;
    }
}
